<?php

// +----------------------------------------------------------------------
// | JiZhiCMS { 极致CMS，给您极致的建站体验 }  
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2099 http://www.jizhicms.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 留恋风 <2581047041@qq.com>
// +----------------------------------------------------------------------
// | Date：2019/10/08
// +----------------------------------------------------------------------


namespace Home\plugins;

use Home\c\CommonController;
use FrPHP\lib\Controller;
use FrPHP\Extend\Page;

class GetDataController extends CommonController
{
	function _init(){
	    header('Access-Control-Allow-Origin:*');
		parent::_init();
		$apidata = M('plugins')->find(['filepath'=>'apidata','isopen'=>1]);
		if(!$apidata){
			JsonReturn(['code'=>1,'msg'=>'插件未开启！']);
		}
		$this->config = json_decode($apidata['config'],1);
		
		$this->checkIp();
		$this->checkKey();
	}
	
	//获取数据处理
	/**
	
		model: 模块名，必填
		key:访问秘钥，必填
		where:查询条件，默认查询该模块所有内容，根据请求，自动获取
		orders:查询排序，默认id倒序，字符串格式： 'addtime desc' ，可以不填
		limit:显示条数，默认查询所有，数字，正整数，可以不填
		fields:获取字段，默认查询所有，字符串格式： 'id,classname,tid' ，可以不填
	
	**/
	function index(){
		
		$tables = explode(',',$this->config['tables']);
		$table = $this->frparam('model',1,false);
		if($table && is_array($tables) && in_array($table,$tables)){
			$model = $this->frparam('model',1,false);
			
			$orders = $this->frparam('orders',1,' id desc ');
			$limit = $this->frparam('limit',0,NULL);
			$fields = $this->frparam('fields',1,null);
			$page = $this->frparam('page',0,1);
			if(!$model){
				JsonReturn(['code'=>1,'msg'=>'model参数错误！']);
			}
			if($page>1){
			    $limit = ($page-1)*$limit.','.$limit;
			}
			if($model=='sysconfig'){
				$limit = null;
			}
			
			$data = $this->frparam();
			unset($data['model']);
			unset($data['orders']);
			unset($data['limit']);
			unset($data['fields']);
			unset($data['key']);
			$where = null;
			foreach($data as $k=>$v){
				$where[$k] = $this->frparam($k,1);
			}
			$res = M($model)->findAll($where,$orders,$fields,$limit);
			JsonReturn(['code'=>0,'data'=>$res]);
			
		}else{
			JsonReturn(['code'=>1,'msg'=>'数据不存在！']);
		}
		
	}

    function getclass(){

        $tables = explode(',',$this->config['tables']);
        $table = $this->frparam('model',1,false);
        if($table && is_array($tables) && in_array($table,$tables)){
            $data = $this->frparam();
            $orders = $this->frparam('orders',1,' orders desc ');
            $limit = $this->frparam('limit',0,NULL);
            $fields = $this->frparam('fields',1,null);
            $page = $this->frparam('page',0,1);
            if(!$table){
                JsonReturn(['code'=>1,'msg'=>'model参数错误！']);
            }
            if($page>1){
                $limit = ($page-1)*$limit.','.$limit;
            }


            $sql = '';
            if($this->frparam('id') && $this->frparam('isall')){
                $sql = '  id in ('.implode(',',$this->classtypedata[$this->frparam('id')]['children']['ids']).') ';
            }else if($this->frparam('id')){
                $sql = 'id='.$this->frparam('id');
            }
            if($sql){
                $res = M('classtype')->findAll($sql,$orders,$fields,$limit);
            }else{
                $res = [];
            }



            JsonReturn(['code'=>0,'data'=>$res]);

        }else{
            JsonReturn(['code'=>1,'msg'=>'数据不存在！']);
        }

    }

    function hits(){
        $tables = explode(',',$this->config['tables']);
        $table = $this->frparam('model',1,false);
        if($table && is_array($tables) && in_array($table,$tables)){
            $model = $this->frparam('model',1,false);
            $id = $this->frparam('id');
            if(!$model){
                JsonReturn(['code'=>1,'msg'=>'model参数错误！']);
            }
            if(!$id){
                JsonReturn(['code'=>1,'msg'=>'缺少ID参数！']);
            }
            M($model)->goInc(['id'=>$id],'hits');
            JsonReturn(['code'=>0,'msg'=>'success','data'=>M($model)->getField(['id'=>$id],'hits')]);
        }else{
            JsonReturn(['code'=>1,'msg'=>'数据不存在！']);
        }
    }

	function getDataPage(){
	    $tables = explode(',',$this->config['tables']);
		$table = $this->frparam('model',1,false);
		if($table && is_array($tables) && in_array($table,$tables)){
			$model = $this->frparam('model',1,false);
			
			$orders = $this->frparam('orders',1,' id desc ');
			$limit = $this->frparam('limit',0,10);
			$fields = $this->frparam('fields',1,null);
			$page = $this->frparam('page',0,1);
			if(!$model){
				JsonReturn(['code'=>1,'msg'=>'model参数错误！']);
			}
			
			$data = $this->frparam();
			unset($data['model']);
			unset($data['orders']);
			unset($data['limit']);
			unset($data['fields']);
			unset($data['key']);
		
			$classtypedata = classTypeData();
    		foreach($classtypedata as $k=>$v){
    			$classtypedata[$k]['children'] = get_children($v,$classtypedata);
    		}
			$sql = ' isshow=1 ';
		    if($this->frparam('tid')){
		        $sql .= ' and tid in ('.implode(',',$classtypedata[$this->frparam('tid')]['children']['ids']).') ';
		    }else{
		        $sql = null;
    			foreach($data as $k=>$v){
    				$sql[$k] = $this->frparam($k,1);
    			}
		    }
		
			$obj = new Page($model);
			$res = $obj->where($sql)->orderby($orders)->limit($limit)->page($page)->go();
            $sum = $obj->sum;//总数据
            $listpage = $obj->listpage;//分页数组-自定义分页可用
            $prevpage = $obj->prevpage;//上一页
            $nextpage = $obj->nextpage;//下一页
            $allpage = $obj->allpage;//总页数
			JsonReturn(['code'=>0,'data'=>$res,'sum'=>$sum,'listpage'=>$listpage,'prevpage'=>$prevpage,'nextpage'=>$nextpage,'allpage'=>$allpage]);
			
		}else{
			JsonReturn(['code'=>1,'msg'=>'数据不存在！']);
		}
	}
	
	function getDataSearch(){
	    $tables = explode(',',$this->config['tables']);
		$table = $this->frparam('model',1,false);
		if($table && is_array($tables) && in_array($table,$tables)){
			$model = $this->frparam('model',1,false);
			
			$orders = $this->frparam('orders',1,' id desc ');
			$limit = $this->frparam('limit',0,10);
			$fields = $this->frparam('fields',1,null);
			$page = $this->frparam('page',0,1);
			if(!$model){
				JsonReturn(['code'=>1,'msg'=>'model参数错误！']);
			}
			
			$data = $this->frparam();
			unset($data['model']);
			unset($data['orders']);
			unset($data['limit']);
			unset($data['fields']);
			unset($data['key']);
		
			$classtypedata = classTypeData();
    		foreach($classtypedata as $k=>$v){
    			$classtypedata[$k]['children'] = get_children($v,$classtypedata);
    		}
			$sql = ' isshow=1 ';
			
			$search = $this->frparam('search',1);
			$word = $this->frparam('word',1);
			if($search && $word){
			    $s = explode(',',$search);
			    $sql.= ' and (';
			    $sqx = [];
			    foreach ($s as $v){
			        $sqlx[] =" ".$v." like '%".$word."%' ";
			    }
			    $sql.= implode(' or ',$sqlx);
			    $sql.=' ) ';
			}
			
			
		    if($this->frparam('tid')){
		        $sql .= ' and tid in ('.implode(',',$classtypedata[$this->frparam('tid')]['children']['ids']).') ';
		    }
		
			$obj = new Page($model);
			$res = $obj->where($sql)->orderby($orders)->limit($limit)->page($page)->go();
            $sum = $obj->sum;//总数据
            $listpage = $obj->listpage;//分页数组-自定义分页可用
            $prevpage = $obj->prevpage;//上一页
            $nextpage = $obj->nextpage;//下一页
            $allpage = $obj->allpage;//总页数
            JsonReturn(['code'=>0,'data'=>$res,'sum'=>$sum,'listpage'=>$listpage,'prevpage'=>$prevpage,'nextpage'=>$nextpage,'allpage'=>$allpage]);
			
		}else{
			JsonReturn(['code'=>1,'msg'=>'数据不存在！']);
		}
	}
	
	//新增/修改
	function savedata(){
		if(!$this->config['isaddedit'] && $this->config['isaddedit']!=1){
			JsonReturn(['code'=>1,'msg'=>'未开启新增修改权限！']);
		}
		$tables = explode(',',$this->config['tables']);
		$table = $this->frparam('model',1,false);
		if($table && is_array($tables) && in_array($table,$tables)){
			$id = $this->frparam('id',0);
			$w = [];
			
			if($id){
			    
			    $w = $this->frparam();
			    foreach ($w as $k=>$v){
			        if($k=='body'){
			            $w[$k] = $this->frparam($k,4);
			        }else{
			            $w[$k] = $this->frparam($k,1);
			        }
			        
			    }
			    
			    
			    $r = M($table)->update(['id'=>$id],$w);
			}else{
			    	switch($table){
    				case 'article':
    				$w = $this->frparam();
    				$w['title'] = $this->frparam('title',1);
    				$w['seo_title'] = $this->frparam('seo_title',1) ? $this->frparam('seo_title',1) : $w['title'];
    				$w['description'] = $this->frparam('description',1,'');
    				$w['keywords'] = $this->frparam('keywords',1,'');
    				$w['body'] = $this->frparam('body',4);
    				$w['tid'] = $this->frparam('tid',0,0);
    				if($w['tid']){
    					$w['htmlurl'] = $this->classtypedata[$w['tid']]['htmlurl'];
    				}
    			
    				$w = get_fields_data($w,'article');
    				
    				
    				break;
    				case 'product':
    				$w = $this->frparam();
    				$w['title'] = $this->frparam('title',1);
    				$w['seo_title'] = $this->frparam('seo_title',1) ? $this->frparam('seo_title',1) : $w['title'];
    				$w['description'] = $this->frparam('description',1,'');
    				$w['keywords'] = $this->frparam('keywords',1,'');
    				$w['body'] = $this->frparam('body',4);
    				$w['price'] = $this->frparam('price',3,0);
    				$w['stock_num'] = $this->frparam('stock_num',0,20);
    				$w['tid'] = $this->frparam('tid',0,0);
    				if($w['tid']){
    					$w['htmlurl'] = $this->classtypedata[$w['tid']]['htmlurl'];
    				}
    				$w = get_fields_data($w,'product');
    				
    				
    				
    				
    				break;
    				default:
    				
    				$w = $this->frparam();
    				$w = get_fields_data($w,$table);
    				
    				break;
    			}
    			$r = M($table)->add($w);
			}
		
			if($r){
				JsonReturn(['code'=>0,'msg'=>'操作成功！']);
			}else{
				JsonReturn(['code'=>1,'msg'=>'操作失败！']);
			}
		
			
		}else{
			JsonReturn(['code'=>1,'msg'=>'模块参数错误，操作失败！']);
		}
	}

    function message(){
        if(!isset($this->config['ismessage']) || $this->config['ismessage']!=1){
            JsonReturn(['code'=>1,'msg'=>'请先在API数据插件开启留言']);
        }
        if($_POST){

            $w = $this->frparam();
            $w = get_fields_data($w,'message',0);

            $w['body'] = $this->frparam('body',1,'','POST');
            $w['user'] = $this->frparam('user',1,'','POST');
            $w['tel'] = $this->frparam('tel',1,'','POST');
            $w['aid'] = $this->frparam('aid',0,0,'POST');
            $w['tid'] = $this->frparam('tid',0,0,'POST');
            $w['email'] = $this->frparam('email',1,'','POST');
            $w['orders'] = 0;
            $w['istop'] = 0;
            $w['hits'] = 0;

            if($this->webconf['autocheckmessage']==1){
                $w['isshow'] = 1;
            }else{
                $w['isshow'] = 0;
            }




            $w['ip'] = GetIP();
            $w['addtime'] = time();
            if(isset($_SESSION['member'])){
                $w['userid'] = $_SESSION['member']['id'];
            }else{
                $w['userid'] = 0;
            }

            if($this->frparam('title',1,'','POST')==''){
                JsonReturn(['code'=>1,'msg'=>JZLANG('标题不能为空！')]);
            }
            if($w['user']==''){
                JsonReturn(['code'=>1,'msg'=>'称呼不能为空！']);
            }


            $w['title'] = $this->frparam('title',1);
            //仅在存在手机号的情况进行检测手机号是否有效-可自由设置
            if($w['tel']){
                if(!preg_match("/^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\\d{8}$/",$w['tel'])){
                    //$this->error('您的手机号格式不正确！');
                    JsonReturn(['code'=>1,'msg'=>JZLANG('您的手机号格式不正确！')]);
                }

            }
            // 不为空检测
            $sql = " molds='message' and isshow=1 ";
            $fields_list = M('Fields')->findAll($sql,'orders desc,id asc');
            if($fields_list){
                foreach($fields_list as $v){
                    if($v['ismust']==1){
                        if($w[$v['field']]==''){
                            if(in_array($v['fieldtype'],array(6,10))){
                                if($w[$v['field'].'_urls']==''){

                                    JsonReturn(['code'=>1,'msg'=>$v['fieldname'].JZLANG('不能为空！')]);
                                }
                            }else{
                                JsonReturn(['code'=>1,'msg'=>$v['fieldname'].JZLANG('不能为空！')]);
                            }

                        }
                    }
                }
            }




            if(!isset($_SESSION['message_time'])){
                $_SESSION['message_time'] = time();
                $_SESSION['message_num'] = 0;
            }

            if(($_SESSION['message_time']+10*60)<time()){
                $_SESSION['message_num'] = 0;
                $_SESSION['message_time'] = time();
            }
            $_SESSION['message_num']++;
            if($_SESSION['message_num']>5 && ($_SESSION['message_time']+10*60)>=time()){
                JsonReturn(['code'=>0,'msg'=>JZLANG('您操作过于频繁，请10分钟后再尝试！')]);
            }


            $res = M('message')->add($w);
            if($res){
                JsonReturn(['code'=>0,'msg'=>JZLANG('提交成功！我们会尽快回复您！'),'url'=>get_domain()]);
            }else{
                JsonReturn(['code'=>1,'msg'=>JZLANG('提交失败，请重试！')]);
            }



        }



    }


    //IP审核
	function checkIp(){
		$ip = GetIP();//获取IP
		if($this->config['ischeckip']==1){
			$iplist = explode('||',$this->config['iplist']);
			if(!is_array($iplist) || !in_array($ip,$iplist)){
				JsonReturn(['code'=>1,'msg'=>'IP不在白名单！']);
			}
		}
		
	}
	//秘钥审核
	function checkKey(){
		$key = $this->config['key'];
		$current_key = $this->frparam('key',1);
		if($key!=$current_key){
			JsonReturn(['code'=>1,'msg'=>'秘钥错误！']);
		}
	}
	
	
}